﻿using CPPEI.Coolzon.Service.Contract;
using CPPEI.Coolzon.ServiceProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// Interaction logic for SelectSolutionView.xaml
    /// 选择方案视图
    /// </summary>
    public partial class SelectSolutionView_Old : View
    {
        private string _preSolutionName;
        public string SelectedSolutionName { get; private set; }

        public SelectSolutionView_Old()
        {
            InitializeComponent();

            _ctlListSolution.DropDownClosed += _ctlListSolution_DropDownClosed;
            //_ctlListDomain.DropDownClosed += _ctlListDomain_DropDownClosed;

            Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Register(this);

        }

        public SelectSolutionView_Old(string solutionName)
            : this()
        {
            _preSolutionName = solutionName;
        }

        protected override void OnActive()
        {
            // 构造获取方案列表的请求
            Service.Contract.ReqGetSolutionListData requestData = new Service.Contract.ReqGetSolutionListData();

            CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                UIContext.Current.ControlDomainId,
                UIContext.Current.UserIdentity,
                Service.Contract.REQUEST_SCENARIO.CONNENT,
                Service.Contract.REQUEST_COMMAND.CONNECT.GETSOLUTIONLIST,
                Service.Contract.REQUEST_MODE.SELF,
                requestData);
            ClientProxyManager.Instance.SendRequestSys(request);

            base.OnActive();
        }

        [Common.Subscribe]
        public void OnReceiveGetSolutionListData(Service.Contract.RespGetSolutionListData data)
        {
            if (data != null)
            {
                _ctlListSolution.ItemsSource = data.Solutions;

                // 加在预先选择的方案
                if (!string.IsNullOrEmpty(_preSolutionName))
                {
                    for (int index = 0; index < data.Solutions.Length; index++)
                    {
                        Service.Contract.SolutionItem solution = data.Solutions[index];
                        if (solution.Name == _preSolutionName)
                        {
                            _ctlListSolution.SelectedItem = solution;
                        }
                    }
                }
            }
        }

        void _ctlListSolution_DropDownClosed(object sender, EventArgs e)
        {
            Service.Contract.SolutionItem selSolution = _ctlListSolution.SelectedItem as Service.Contract.SolutionItem;
            if (selSolution != null)
            {
                // 构造获取控制域列表的请求
                Service.Contract.ReqGetDomainListData requestData = new Service.Contract.ReqGetDomainListData();
                requestData.SolutionName = selSolution.Name;

                CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                    UIContext.Current.ControlDomainId,
                    UIContext.Current.UserIdentity,
                    Service.Contract.REQUEST_SCENARIO.CONNENT,
                    Service.Contract.REQUEST_COMMAND.CONNECT.GETDOMAINLIST,
                    Service.Contract.REQUEST_MODE.SELF,
                    requestData);
                ClientProxyManager.Instance.SendRequestSys(request);
            }
        }

        [Common.Subscribe]
        public void OnReceiveGetDomainListData(Service.Contract.RespGetDomainListData data)
        {
            if (data != null)
            {
                _ctlListDomain.ItemsSource = data.Domains;
            }
        }

        /// <summary>
        /// 点击确定按钮，加入控制域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _ctlBtnOK_Click(object sender, RoutedEventArgs e)
        {
            SolutionItem selSolution = _ctlListSolution.SelectedItem as SolutionItem;
            DomainItem selDomain = _ctlListDomain.SelectedItem as DomainItem;
            if (selSolution != null && selDomain != null)
            {
                // 构造进入控制域的请求
                Service.Contract.ReqEnterDomainData requestData = new Service.Contract.ReqEnterDomainData();
                requestData.DomainId = selDomain.Id;
                requestData.SolutionName = selSolution.Name;

                CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                    UIContext.Current.ControlDomainId,
                    UIContext.Current.UserIdentity,
                    Service.Contract.REQUEST_SCENARIO.CONNENT,
                    Service.Contract.REQUEST_COMMAND.CONNECT.ENTERDOMAIN,
                    Service.Contract.REQUEST_MODE.SELF,
                    requestData);
                ClientProxyManager.Instance.SendRequest(request);
            }
        }

        [Common.Subscribe]
        public void OnReceiveEnterDomainData(Service.Contract.RespEnterDomainData data)
        {
            SolutionItem selSolution = _ctlListSolution.SelectedItem as SolutionItem;
            DomainItem selDomain = _ctlListDomain.SelectedItem as DomainItem;
            if (data != null)
            {
                if (data.IsSuccess)
                {
                    UIContext.Current.ControlDomainId = selDomain.Id;
                    SelectedSolutionName = selSolution.Name;
                    this.Dispatcher.Invoke(() =>
                    {
                        Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(this);
                        this.Submit();
                    });
                }
                else
                {
                    MessageBox.Show(data.Message);
                }
            }
        }

        private void _ctlBtnCancel_Click(object sender, RoutedEventArgs e)
        {
            Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(this);
            this.Cancel();
        }

        private void _ctlBtnNewDomain_Click(object sender, RoutedEventArgs e)
        {
            SolutionItem selSolution = _ctlListSolution.SelectedItem as SolutionItem;
            if (selSolution != null)
            {
                Windows.CreateDomainWindow createWindow = new Windows.CreateDomainWindow();
                if (createWindow.ShowDialog() == true)
                {
                    // 构造创建控制域的请求
                    Service.Contract.ReqCreateDomainData requestData = new Service.Contract.ReqCreateDomainData();
                    requestData.SolutionCaption = selSolution.Caption;
                    requestData.SolutionName = selSolution.Name;
                    requestData.DomainCaption = createWindow.ControlDomainCaption;

                    CPPEI.Coolzon.ServiceProxy.MainServiceRef.Request request = RequestUtil.BuildRequest(
                        UIContext.Current.ControlDomainId,
                        UIContext.Current.UserIdentity,
                        Service.Contract.REQUEST_SCENARIO.CONNENT,
                        Service.Contract.REQUEST_COMMAND.CONNECT.CREATEDOMAIN,
                        Service.Contract.REQUEST_MODE.SELF,
                        requestData);
                    ClientProxyManager.Instance.SendRequest(request);
                }
            }
        }

        [Common.Subscribe]
        public void OnReceiveCreateDomainData(Service.Contract.RespCreateDomainData data)
        {
            SolutionItem selSolution = _ctlListSolution.SelectedItem as SolutionItem;

            if (data != null && data.IsSuccess)
            {
                UIContext.Current.ControlDomainId = data.DomainId;
                SelectedSolutionName = selSolution.Name;

                this.Dispatcher.Invoke(() =>
                {
                    Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(this);
                    this.Submit();
                });
            }
        }

    }
}
